﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Com.Crc.ResManage.BLL.EquipManage;
using Com.App_Code;
using Infragistics.WebUI.UltraWebGrid;
using System.Collections;

namespace Com.Crc.ResManage.Web.EquipManage.PON
{
    public partial class PONManage:BasePage
    {
        OLTManageBLL bll;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                bll = new OLTManageBLL();
                InitControl();
            }
        }

        private void InitControl()
        {
            this.btnAdd.Attributes.Add("onclick", "return OpenAddWindow();");
            this.btnDelete.Attributes.Add("onclick", "return window.confirm('删除OLT设备会同时删除PON板以及所挂用户，确认删除?')");

            btnQuery_Click(null,null);
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            bll = new OLTManageBLL();
            string strBranchOfficeID = "";
            string strOfficeID = "";
            if (this.ddlBranchOffice.SelectedIndex != -1)
            {
                strBranchOfficeID = this.ddlBranchOffice.Items[this.ddlBranchOffice.SelectedIndex].Value;
            }
            if (this.ddlOffice.SelectedIndex != -1)
            {
                strOfficeID = this.ddlOffice.Items[this.ddlOffice.SelectedIndex].Value;
            }
            DataTable dt = bll.QueryOLTData(strBranchOfficeID, strOfficeID, "");
            BindData(dt);

        }

        protected void grd_InitializeLayout(object sender, Infragistics.WebUI.UltraWebGrid.LayoutEventArgs e)
        {
            grd.Bands[0].Columns.Clear();
            int i = 0;
            base.AddGridCheckColumn(this.grd, i++, grd.ClientID);
            base.AddGridColumn(this.grd, "N_ID", "N_ID", 80, i++, true);
            base.AddGridColumn(this.grd, "所属分局", "S_BRANCH_NAME", 90, i++, false);
            base.AddGridColumn(this.grd, "局点名称", "S_SUBSTATION_NAME", 100, i++, false);
            base.AddGridColumn(this.grd, "设备型号", "S_OLTTYPE", 80, i++, false);
            base.AddGridColumn(this.grd, "设备名称", "S_NAME", 80, i++, false);
            base.AddGridColumn(this.grd, "管理地址", "S_ADDRESS", 80, i++, false);
            base.AddGridColumn(this.grd, "管理VLAN", "N_VLAN", 80, i++, false);
            base.AddGridColumn(this.grd, "上行带宽", "S_BANDWIDTH_TYPE", 80, i++, false);
            base.AddGridColumn(this.grd, "上端BAS", "S_BAS_TYPE", 80, i++, false);
            base.AddGridColumn(this.grd, "BAS端口", "N_UP_BAS_PORT", 80, i++, false);
            base.AddGridColumn(this.grd, "上端交换机", "S_EXCHANGE_NAME", 80, i++, false);
            base.AddGridColumn(this.grd, "上端交换机端口", "N_UP_EXCANGE_BRT", 80, i++, false);

            base.AddGridImageColumn(grd, "编辑", "Edit", 40, i++, "../../../../../img/icon_edit.gif");

            //this.grd.Columns.FromKey("S_NAME").Width = Unit.Percentage(100);
        }

        private void BindData(DataTable dt)
        {
            grd.DataSource = dt;
            grd.DataBind();
        }

        protected void grd_PageIndexChanged(object sender, Infragistics.WebUI.UltraWebGrid.PageEventArgs e)
        {

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            ArrayList arrEquipID = new ArrayList();
            foreach (UltraGridRow gvRow in this.grd.DisplayLayout.Rows)
            {
                if (gvRow.Cells.FromKey("CHECK").ToString().ToUpper() == "TRUE")
                {
                    int ID = int.Parse(gvRow.Cells.FromKey("n_id").ToString());
                    arrEquipID.Add(ID.ToString());
                }
            }

            if (arrEquipID.Count == 0)
            {
                ShowAlertForUpdatePanel("请选择设备!");
                return;
            }

            bll = new OLTManageBLL();
            bll.DeleteOLTData(arrEquipID);
            btnQuery_Click(null, null);
            ShowAlertForUpdatePanel("设备删除成功!");
        }
    }
}